unit uniManMovimentacaoEstoque;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Mask, StdCtrls, DBCtrls, ExtCtrls, Buttons, Grids, DBGrids, DB,
  ADODB;

type
  TfrmManMovimentacaoEstoque = class(TForm)
    Panel4: TPanel;
    DBGrid1: TDBGrid;
    Panel2: TPanel;
    btnFechar: TSpeedButton;
    btnIncluir: TSpeedButton;
    Bevel1: TBevel;
    Panel3: TPanel;
    Label2: TLabel;
    btnPesquisa: TSpeedButton;
    edtCodigo: TEdit;
    Label1: TLabel;
    dblMaterial: TDBLookupComboBox;
    Label3: TLabel;
    rbTodas: TRadioButton;
    rbEntrada: TRadioButton;
    rbSaida: TRadioButton;
    Label4: TLabel;
    edtDtFinal: TMaskEdit;
    Label5: TLabel;
    DS_Grid: TDataSource;
    QR_Grid: TADOQuery;
    edtDtInicial: TMaskEdit;
    QR_Material: TADOQuery;
    DS_Material: TDataSource;
    DS_MaterialGrid: TDataSource;
    QR_MaterialGrid: TADOQuery;
    QR_Gridcd_movimentacao: TAutoIncField;
    QR_Gridoperacao: TWideStringField;
    QR_Gridcd_material: TIntegerField;
    QR_Gridcd_usuario: TIntegerField;
    QR_GridsDsMaterial: TStringField;
    QR_GridsOperacao: TStringField;
    QR_Griddata: TWideStringField;
    QR_GridsCor: TStringField;
    QR_GridsLargura: TStringField;
    QR_GridsEspessura: TStringField;
    QR_Gridquantidade: TBCDField;
    procedure btnPesquisaClick(Sender: TObject);
    procedure btnFecharClick(Sender: TObject);
    procedure btnIncluirClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure QR_GridCalcFields(DataSet: TDataSet);
  private
    procedure p_Select;
  public
    { Public declarations }
  end;

var
  frmManMovimentacaoEstoque: TfrmManMovimentacaoEstoque;

implementation

uses uniCadMovimentacaoEstoque;

{$R *.dfm}

{ TfrmManMovimentacaoEstoque }

procedure TfrmManMovimentacaoEstoque.p_Select;
var dtFinal : TDateTime;
begin
  QR_Grid.SQl.Clear;
  QR_Grid.SQl.Add(' SELECT * FROM TB_MOVIMENTACAO_ESTOQUE WHERE 1=1 ');

  if edtCodigo.text <> '' then
    QR_Grid.SQl.Add(' AND CD_MOVIMENTACAO = '+edtCodigo.text);

  if dblMaterial.Text <> '' then
    QR_Grid.SQl.Add(' AND CD_MATERIAL = '+IntToStr(dblMaterial.KeyValue));

  if rbEntrada.Checked then
    QR_Grid.SQl.Add(' AND OPERACAO = ''E'' ');
  if rbSaida.Checked then
    QR_Grid.SQl.Add(' AND OPERACAO = ''S'' ');

  if edtDtInicial.Text <> '  /  /    ' then
  begin
    QR_Grid.SQl.Add(' AND DATA >= :pDataInicial ');
    QR_Grid.Parameters.ParamByName('pDataInicial').Value := StrToDate(edtDtInicial.Text);
  end;

  if edtDtFinal.Text <> '  /  /    ' then
  begin
    QR_Grid.SQl.Add(' AND DATA <= :pDataFinal ');
    dtFinal := StrToDate(edtDtFinal.Text) +1;
    QR_Grid.Parameters.ParamByName('pDataFinal').Value := dtFinal ;
  end;
  QR_Grid.Close;
  QR_Grid.Open;
end;

procedure TfrmManMovimentacaoEstoque.btnPesquisaClick(Sender: TObject);
begin
  p_Select;
end;

procedure TfrmManMovimentacaoEstoque.btnFecharClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmManMovimentacaoEstoque.btnIncluirClick(Sender: TObject);
begin
  try
    Application.CreateForm(TfrmCadMovimentacaoEstoque, frmCadMovimentacaoEstoque);
    frmCadMovimentacaoEstoque.ShowModal;
   finally
     frmCadMovimentacaoEstoque.Free;
   end;
end;

procedure TfrmManMovimentacaoEstoque.FormShow(Sender: TObject);
begin
  QR_Material.Close;
  QR_Material.Open;

  QR_MaterialGrid.Close;
  QR_MaterialGrid.Open;
end;

procedure TfrmManMovimentacaoEstoque.QR_GridCalcFields(DataSet: TDataSet);
begin
  if QR_Grid.FieldByName('operacao').AsString = 'E' then
    QR_Grid.FieldByName('sOperacao').AsString := 'ENTRADA'
  else
    QR_Grid.FieldByName('sOperacao').AsString := 'SAIDA';
end;

end.
